/**
 * jims
 */
package com.jims.master.modules.doctor.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.finance.dao.RegistCostDao;
import com.jims.master.modules.finance.entity.RegistCost;
import com.jims.master.modules.med.entity.MedicalCardConsumeDetail;
import com.jims.master.modules.med.entity.MedicalCardConsumeMaster;
import com.jims.master.modules.med.service.MedicalCardConsumeDetailService;
import com.jims.master.modules.med.service.MedicalCardConsumeMasterService;
import com.jims.master.modules.med.service.MedicalCardMasterService;
import com.jims.master.modules.oper.dao.PriceListDao;
import com.jims.master.modules.reg.dao.ClinicForRegistDao;
import com.jims.master.modules.reg.dao.ClinicIndexDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 病人就诊信息Service
 *
 * @author 赵宁
 * @version 2017-02-28
 */
@Service
@Transactional(readOnly = true)
public class ClinicMasterService extends CrudService<ClinicMasterDao, ClinicMaster> {
    @Autowired
    private ClinicForRegistDao clinicForRegistDao;
    @Autowired
    private RegistCostDao registCostDao;
    @Autowired
    private ClinicIndexDao clinicIndexDao;
    @Autowired
    private MedicalCardMasterService medicalCardMasterService;
    @Autowired
    private MedicalCardConsumeMasterService medicalCardConsumeMasterService;
    @Autowired
    private MedicalCardConsumeDetailService medicalCardConsumeDetailService;
    @Autowired
    private PriceListDao priceListDao;

    public ClinicMaster get(String id) {
        return super.get(id);
    }

    public List<ClinicMaster> findList(ClinicMaster clinicMaster) {
        return super.findList(clinicMaster);
    }

    public Page<ClinicMaster> findPage(Page<ClinicMaster> page, ClinicMaster clinicMaster) {
        return super.findPage(page, clinicMaster);
    }

    @Transactional(readOnly = false)
    public int save(ClinicMaster clinicMaster) {
        return super.save(clinicMaster);
    }

    @Transactional(readOnly = false)
    public int delete(ClinicMaster clinicMaster) {
        return super.delete(clinicMaster);
    }

    /**
     * 退号管理查询挂号信息
     * @param page
     * @param clinicMaster
     * @return page
     * @author 赵宁
     */
    public Page<ClinicMaster> searchClinicRegInfo(Page<ClinicMaster> page, ClinicMaster clinicMaster) {
        clinicMaster.setPage(page);
        clinicMaster.updateOrgId();
        page.setList(dao.searchClinicRegInfo(clinicMaster));
        return page;
    }

    /**
     * 退号信息保存
     * @param clinicMaster
     * @param userId
     * @return int
     * @author 赵宁
     */
    @Transactional(readOnly = false)
    public int backReg(ClinicMaster clinicMaster, String userId) {
        //更新号表
        String registType = clinicMaster.getRegistType();
        String clinicId = clinicMaster.getRegistId();
        if (registType.equals("0")) {
            clinicForRegistDao.updateResistByBack(clinicId);
        } else if (registType.equals("1")) {
            clinicForRegistDao.updateAppointSum(clinicId);
        }
        //更新病人就诊信息
        clinicMaster.setClinicType("2");
        clinicMaster.setRegistDate(new Date());
        clinicMaster.setRegistOperator(userId);
        int code = dao.updateCMByBackReg(clinicMaster);
        RegistCost registCost = new RegistCost();
        registCost.setClinicId(clinicMaster.getId());
        List<RegistCost> registCosts= registCostDao.findList(registCost);
        if(registCosts.size()>0){
             for(int i=0;i<registCosts.size();i++){
                  RegistCost oldReg= registCosts.get(i);
                  RegistCost newRegist = new RegistCost();
                  newRegist.setClinicId(oldReg.getClinicId());
                  newRegist.setCost(-oldReg.getCost());
                  newRegist.setSubjCode(oldReg.getSubjCode());
                  newRegist.setOrgId(oldReg.getOrgId());
                  newRegist.setType(oldReg.getType());
                  newRegist.preInsert();
                  registCostDao.insert(newRegist);
             }
        }
        if ("3".equals(clinicMaster.getType())) {
            ClinicMaster clinicMaster1 = super.get(clinicMaster.getId());
            medicalCardMasterService.returnFee(clinicMaster.getCardNo(), clinicMaster.getCharge());
            //2 消费记录增加一条负的记录
            String feeId = clinicMaster1.getRegistId();//号表Id 对应 medical_card_master的feeid
            MedicalCardConsumeMaster medicalCardConsumeMaster = medicalCardConsumeMasterService.getByfeeId(clinicMaster.getId());
            if (medicalCardConsumeMaster != null) {
                medicalCardConsumeMaster.setId("");
                medicalCardConsumeMaster.setFeeFlag("1");//退费
                medicalCardConsumeMaster.setConsumeMoney(-medicalCardConsumeMaster.getConsumeMoney());
                code += medicalCardConsumeMasterService.save(medicalCardConsumeMaster);
                List<MedicalCardConsumeDetail> list = medicalCardConsumeDetailService.getByMasterId(medicalCardConsumeMaster.getId());
                for (MedicalCardConsumeDetail m : list) {
                    m.setId("");
                    m.setConsumeMoney(-m.getConsumeMoney());
                    m.setAmount(-m.getAmount());
                    code += medicalCardConsumeDetailService.save(m);
                }
            } else {
                return 0;
            }
        }

        return code;
    }

    /**
     * @return
     * @Author chenxy
     */
    public List<PatMasterIndex> getPatsByDoctor(PatMasterIndex patMasterIndex) {
        return dao.getPatsByDoctor(patMasterIndex);
    }

    /**
     * 挂号单打印
     *
     * @param clinicMaster
     * @return
     * @author chentao
     */
    public ClinicMaster clinicMasterPrint(ClinicMaster clinicMaster) {
        return dao.clinicMasterPrint(clinicMaster);
    }

    /**
     * 挂号结账--查询 挂号数量有关数据
     *
     * @param clinicMaster
     * @return
     * @author chentao
     */
    public ClinicMaster clinicCheckout(ClinicMaster clinicMaster) {
        return dao.clinicCheckout(clinicMaster);
    }

    /**
     * 挂号结账--查询 支付方式金额
     * @param clinicMaster
     * @return List<ClinicMaster>
     * @author 赵宁
     */
    public List<ClinicMaster> findCharges(ClinicMaster clinicMaster) {
        return dao.findCharges(clinicMaster);
    }

    /**
     * 病人列表分页
     *
     * @param
     * @return
     */
    public Page<ClinicMaster> findPatientByConditionsPage(Page<ClinicMaster> clinicMasterPage, ClinicMaster clinicMaster) {
        clinicMaster.updateOrgId();
        clinicMaster.setPage(clinicMasterPage);
        clinicMasterPage.setList(dao.findPatientByConditions(clinicMaster));
        return clinicMasterPage;
    }


    public ClinicMaster getById(String id) {
        return dao.getById(id);
    }


    /**
     * 根据门诊号获取病人就诊信息
     * @param clinicNo
     * @param orgId
     * @return ClinicMaster
     * @author 赵宁
     */
    public ClinicMaster getMaster(String clinicNo, String orgId) {
        return dao.getMasterByClinicNo(clinicNo, orgId);
    }
    /**
     * 根据病人Id查询门诊就诊记录
     * @param patId
     * @return
     * @Author
     */
    @Transactional(readOnly = false)
    public ClinicMaster findClinicMasterByPatId(String patId) {
        return dao.findClinicMasterByPatId(patId);
    }
    /**
     * 根据病人Id修改门诊就诊记录
     * @param clinicMaster
     * @return
     * @Author
     */
    @Transactional(readOnly = false)
    public int updateClinicType(ClinicMaster clinicMaster) {
        return dao.updateClinicType(clinicMaster);
    }
}